package lablayer.model.MathComponents;

import lablayer.model.LabData;

/**
 * @author Darkness
 */

class DifferenceBetween implements MathComponent {
    private LabData result;

    private LabData data;

    private String componentName = "Различие между средними значениями выборок";

    private String componentId = "10";

    public DifferenceBetween() {
        this.result = new LabData();
    }

    @Override
    public LabData getData() {
        return this.data;
    }

    @Override
    public void calculate() {
        LabData firstSample = null;
        LabData secondSample = null;

        if (this.data.getData().length != 0) {
            if (this.data.getData().length >= 1) {
                firstSample = new LabData((this.data.getData())[0][0]);
                secondSample = new LabData((this.data.getData())[0][1]);

                MathComponent middleValue = MathComponentFactory.createMiddleValue();
                MathComponent standardError = MathComponentFactory.createStandardAMSError();
                Double fsMiddle, fsError;
                Double ssMiddle, ssError;

                middleValue.setData(firstSample);
                middleValue.calculate();
                fsMiddle = middleValue.getResult().getData()[0][0][0];
                middleValue.setData(secondSample);
                middleValue.calculate();
                ssMiddle = middleValue.getResult().getData()[0][0][0];

                standardError.setData(firstSample);
                standardError.calculate();
                fsError = standardError.getResult().getData()[0][0][0];
                standardError.setData(secondSample);
                standardError.calculate();
                ssError = standardError.getResult().getData()[0][0][0];

                Double middleDiff = ((fsMiddle - ssMiddle) < 0) ?
                    (ssMiddle - fsMiddle) : (fsMiddle - ssMiddle);
                Double errorsValue = Math.sqrt(Math.pow(fsError, 2) + Math.pow(ssError, 2));
                this.result.setData(middleDiff / errorsValue);
            } else {
                throw new UnsupportedOperationException();
            }
        } else {
            throw new UnsupportedOperationException("data's length is null");
        }
    }

    @Override
    public void setData(LabData data) {
        if (data != null) {
            this.data = data;
        } else {
            throw new NullPointerException("data is nullpointer");
        }
    }

    @Override
    public String getComponentId() {
        return this.componentId;
    }

    @Override
    public String getComponentName() {
        return this.componentName;
    }

    @Override
    public LabData getResult() {
        return this.result;
    }

	@Override
	public void setParameter(String params, String values) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public LabData getInitData() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

}
